package com.ruoyi.goods.excel;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.vo.GoodsQueryVo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.goods.excel.vo.GoodsOrderVo;
import com.ruoyi.goods.excel.vo.GoodsVo;
import com.ruoyi.goods.server.GoodsExcelService;
import com.ruoyi.system.domain.GoodsChoice;
import com.ruoyi.system.domain.GoodsOrder;
import com.ruoyi.system.service.GoodsChoiceService;
import com.ruoyi.system.service.GoodsOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goods")
public class GoodsExcelController {

    @Autowired
    private GoodsExcelService goodsExcelService;
    @Autowired
    private GoodsChoiceService goodsChoiceService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private GoodsOrderService goodsOrderService;


    /**
     * Excel 文件上传添加商品
     *
     * @param excelFile excel 文件
     * @return
     */
    @PostMapping("/excel/add")
    public AjaxResult uploadGoods(MultipartFile excelFile, Boolean updateSupport) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        ExcelUtil<GoodsVo> goodsVoExcelUtil = new ExcelUtil<>(GoodsVo.class);
        List<GoodsVo> goodsVos = goodsVoExcelUtil.importExcel(excelFile.getInputStream());
        goodsVos = goodsVos.stream().peek(item -> item.setShopDsr(loginUser.getUser().getUserId().toString())).collect(Collectors.toList());
        goodsExcelService.uploadGoods(goodsVos);
        return AjaxResult.success();
    }

    /**
     * 导出订单商品
     */
    @GetMapping("/excel/export/order")
    public AjaxResult exportOrder(
            @RequestParam(required = false, defaultValue = "") String status,
            @RequestParam(required = false, defaultValue = "") String phone,
            @RequestParam(required = false, defaultValue = "") String title
    ) {
        List<com.ruoyi.system.domain.vo.GoodsOrderVo> goodsOrders = goodsOrderService.adminQueryAllByLimit(status, phone, title);
        ExcelUtil<GoodsOrderVo> goodsChoiceExcelUtil = new ExcelUtil<>(GoodsOrderVo.class);
        ArrayList<GoodsOrderVo> goodsOrderVos = new ArrayList<>();
        goodsOrders.forEach(item -> {
            GoodsOrderVo goodsOrderVo = new GoodsOrderVo();
            BeanUtils.copyProperties(item, goodsOrderVo);
            goodsOrderVos.add(goodsOrderVo);
        });
        return goodsChoiceExcelUtil.exportExcel(goodsOrderVos, "订单信息");
    }

    /**
     * 导出全部商品excel
     */
    @GetMapping("/excel/export")
    public AjaxResult exportExcel() {
        GoodsQueryVo goodsQueryVo = new GoodsQueryVo();
        List<GoodsChoice> goodsChoices = goodsChoiceService.queryAllByLimit(goodsQueryVo);
        ExcelUtil<GoodsChoice> goodsChoiceExcelUtil = new ExcelUtil<>(GoodsChoice.class);
        return goodsChoiceExcelUtil.exportExcel(goodsChoices, "商品信息");
    }
}
